package com.kx.yooli.kx.wechat.utils.jasypt;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author john_sqwen
 * @date 2024/3/19
 * @PROJECT_NAME wechat
 * @description create JasyptUtils by john_sqwen
 * and string encryption value is
 */
@Configuration
public class JasyptUtils {
    private final static Logger log = LoggerFactory.getLogger(JasyptUtils.class);
    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor(){
        PooledPBEStringEncryptor pooledPBEStringEncryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig simpleStringPBEConfig = new SimpleStringPBEConfig();
        simpleStringPBEConfig.setPassword("kxwechatMD5");
        //simpleStringPBEConfig.setPassword("111");
        simpleStringPBEConfig.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        simpleStringPBEConfig.setKeyObtentionIterations("1000");
        simpleStringPBEConfig.setPoolSize("1");
        simpleStringPBEConfig.setProviderName("SunJCE");
        simpleStringPBEConfig.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        simpleStringPBEConfig.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        simpleStringPBEConfig.setStringOutputType("base64");
        pooledPBEStringEncryptor.setConfig(simpleStringPBEConfig);
        return pooledPBEStringEncryptor;
    }
    public  static String encryptor(String args){
        if(args.isBlank()){
            log.info("input param args value is {}",args);
            return null;
        }
        log.info("===================encryption ======================");
        // encryptor
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //encrypt config
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        //encrypt way default encrypt way :PBEWithMD5AndDES , change encrypt way:PBEWithMD5AndTripleDES
        config.setAlgorithm("PBEWithMD5AndDES");
        //encrypt salt
        config.setPassword("kxwechatMD5");
        //use config
        encryptor.setConfig(config);
        String ciphertext=encryptor.encrypt(args);
        log.info("加密后的密码串: " + ciphertext);
        log.info("===================decrypt ======================");
        //decrypt
        String passwd=encryptor.decrypt(ciphertext);
        String passwd1 = encryptor.decrypt(args);
        log.info("已加密后解密为："+passwd1);
        log.info(("解密后的密码串: " + passwd));
        return ciphertext;
    }

    public static void main(String[] args) {
//        String rgs = "huFFKVDOaKcKCra/ryg9bLrjrnJ1p7y+";
//        encryptor(rgs);
        JasyptUtils jasyptUtils = new JasyptUtils();
        //加密
       // String root = jasyptUtils.stringEncryptor().encrypt("10.30.20.91");
       // String root1 = jasyptUtils.stringEncryptor().encrypt("kxwechat");
      //   log.info("加密后的密码串: " + root);
      //   log.info("加密后的密码串: " + root1);
        //解密
        String decrypt1 = jasyptUtils.stringEncryptor().decrypt("Tdt26RaNRSss59ZJumpBrbAynpy4bUQCh9AFr7gDb3WHOYNaesCFtEIC3ogDfvGxyWgi/ULASPmDn32SUHc34g==");
        String decrypt2 = jasyptUtils.stringEncryptor().decrypt("iwxVi294MdDC91/ABSRc7cbrnlnZXzDvo6dPLy7bHYZ6yvCrtRvBhaPJdKMpoIOu");
        String decrypt3 = jasyptUtils.stringEncryptor().decrypt("Wnq2+xP6L+d8wagvfjNdCwjSdD1xYW7K4JyxccJ9RJsH3dnPn+F52OntYadeBoZxUUgK71JbMznHRdhCcWKpLA==");
        String decrypt4 = jasyptUtils.stringEncryptor().decrypt("juorOpNqGb8ySLWZyxvXaPRu2YzchJSdD9fmIqZQyS8Olp7Ehh6SPC5DPrLbCF2G");
        String decrypt5 = jasyptUtils.stringEncryptor().decrypt("0SmBx0NWm7ZJMWQASdvDlL/HVulKm3+YfUFZqHeMyLcZ0dB/vD4SOphjYaXmNXaT");
        //nacos
        //  System.out.println(new BCryptPasswordEncoder().encode("11"));
        log.info(("解密后的密码串: " + decrypt1));
        log.info(("解密后的密码串: " + decrypt2));
        log.info(("解密后的密码串: " + decrypt3));
        log.info(("解密后的密码串: " + decrypt4));
        log.info(("解密后的密码串: " + decrypt5));
    }
}
